package at.ac.tuwien.sepm.s2011.e0208329.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import at.ac.tuwien.sepm.s2011.e0208329.dao.DAOFactoryImpl;
import at.ac.tuwien.sepm.s2011.e0208329.dao.DienstleistungDAO;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAO;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAOImpl;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Rechnung;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyBillServiceImpl;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServices;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServicesImpl;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyOrderService;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyOrderServiceImpl;

@SuppressWarnings("serial")
public class AddOrdersUI extends JFrame {
	private JTextField[] fields;
	private String[] labels;
	private JPanel labelPanel;
	private JPanel fieldPanel;
	private OrderDAO orDAO;
	private DienstleistungDAO serDAO;
	private ArrayList<Dienstleistung> services;
	private int serNum;
	private static final Logger log = Logger.getLogger(AddOrdersUI.class);

	public AddOrdersUI(String name, int billID, String cusName, String cusAdress) {
		super(name);
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		orDAO = DAOFactoryImpl.getDAOFactory().getOrderDAO();
		serDAO = DAOFactoryImpl.getDAOFactory().getServiceDAO();
		try {
			services = serDAO.getAllServices();
		} catch (SQLException e2) {
			log.error(e2.getMessage());
			JOptionPane.showMessageDialog(null, "Database error",
					"Please see log file for more detail !",
					JOptionPane.ERROR_MESSAGE);
		}
		serNum = services.size();
		labelPanel = new JPanel(new GridLayout(serNum + 3, 1));
		fieldPanel = new JPanel(new GridLayout(serNum + 3, 1));
		fields = new JTextField[serNum + 3];
		labels = new String[serNum + 3];
		labels[0] = "RechnungID";
		labels[1] = "Kundenname";
		labels[2] = "Addresse";
		int labIndex = 3;
		for (Dienstleistung ser : services) {
			labels[labIndex] = ser.getName();
			labIndex++;
		}

		for (int i = 0; i < labels.length; i++) {
			fields[i] = new JTextField();
			fields[i].setColumns(25);
			JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
			lab.setLabelFor(fields[i]);
			labelPanel.add(lab);
			JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
			p.add(fields[i]);
			fieldPanel.add(p);
		}
		fields[0].setText("" + billID);
		fields[0].setEnabled(false);
		fields[1].setText(cusName);
		fields[1].setEnabled(false);
		fields[2].setText(cusAdress);
		fields[2].setEnabled(false);
		JButton submit = new JButton("Bestellung speichern");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int i = 3; i < fields.length; i++) { // read the text-fields
					try {
						String serviceName = labels[i];
						int number = Integer.parseInt(fields[i].getText());
						int serviceID;
						serviceID = MyCarServicesImpl.getInstance()
								.getServiceID(serviceName);
						int myBillID = Integer.parseInt(fields[0].getText());
						log.info("Adding " + number + "of service " + serviceID + " to bill " + myBillID);
						MyOrderServiceImpl.getInstance().order(myBillID, serviceID,
								number);//run MyOrderServiceImpl.order() to add single entry to database's table "RechnungItem"
					} catch (SQLException e1) {
						log.error(e1.getMessage());
						JOptionPane.showMessageDialog(null, "Please see log file for more detail !", "Bestellung speichern: Database error",
								JOptionPane.ERROR_MESSAGE);
					} catch (NumberFormatException e2) {
						log.error(e2.getMessage());
						JOptionPane.showMessageDialog(null, "Please see log file for more detail !", "Input invalid",
								JOptionPane.ERROR_MESSAGE);
					}

				}
				JOptionPane.showMessageDialog(null, "Bestellungen von Dienstleistungen wurde erfolgreich getaetigt !",
						"Erfolg !",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(submit);
		this.getContentPane().add(labelPanel, BorderLayout.WEST);
		this.getContentPane().add(fieldPanel, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		this.pack();
	}

}
